package com.aier.user.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.common.AndroidType;
import com.aier.admin_new.service.IChannelClient;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.user.entity.Follow;
import com.aier.user.entity.FollowGroup;
import com.aier.user.entity.Fresh;
import com.aier.user.service.IHomeForumsService;
import com.aier.user.service.IMyFollowService;
import com.aier.user.service.IUserService;
import com.baidu.yun.channel.model.BindInfo;

@Scope("prototype")
@Component("userMyFollowAction")
public class UserMyFollowAction extends UserBaseAction{

    /**
     * 我关注的,和密友有区分
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name="myFollowService")
    private IMyFollowService myFollowService;
    @Resource(name="homeForumsService")
    private IHomeForumsService homeForumsService;
    @Resource(name="channelClient")
    private IChannelClient channelClient;
    @Resource(name = "userService")
    private IUserService userService;
    
    // 错误码
    private int s;
    // 当前页数
    private int page;
    // 每页的记录数
    private int page_size;
    // 排序字段
    private String sort;
    // 总条数
    private int count;
    // 总页数
    private int page_count;
    
    // 所有的关注分组
    private List<FollowGroup> fgList;
    // 所有关注总数
    private int followCount;
    
    // 所有的关注
    private List<Follow> followList;
    
    // 取消关注的用户id
    private String fuidStr;
    
    // 新增关注的用户id
    private Long fuid;
    
    // 分组id
    private Long fgid;
    
    // 分组名称
    private String name;
    
    // 新鲜事列表
    private List<Fresh> freshList;
    
    private Long uid;
    // 求关注内容
    private String content;
    
    // 关注状态：1已关注；2互相关注;0全部
    private Short state;
    
    // 访问其他人主页时
    private Long in_uid;
    
    // 关注的昵称列表（@时用）
    private List<String> nicknameList;
    
    Follow follow;
    
    @Override
    public String execute(){
        
        return SUCCESS;
    }
    
    /**
     * 获取所有的关注分组以及每个分组的人数
     * @return
     */
    public String searchFollowGroup() {
        fgList = myFollowService.searchFollowGroup(
                (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        followCount = 0;
        for (FollowGroup fg:fgList) {
            followCount += fg.getCount();
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取所有的关注
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAllFollow() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        
        Map<String, Object> map = null;
        if (null != in_uid) {
            map = myFollowService.searchAllFollow(page, page_size, in_uid,fgid,sort,state);
        } else {
            map = myFollowService.searchAllFollow(page, page_size, 
                    (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),fgid,sort,state);
        }
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer)map.get(ICommonDAO.COUNT);
            page = (Integer)map.get(ICommonDAO.PAGE);
            followList = (List<Follow>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 批量取消关注
     * @return
     */
    public String cancelFollow() {
        s = myFollowService.deleteFollow(uid, fuidStr);
        return SUCCESS;
    }
    
    /**
     * 添加关注
     * @return
     */
    public String addFollow() {
        s = myFollowService.insertFollow((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fuid);
        return SUCCESS;
    }
    
    /**
     * 将关注或密友移动分组
     * @return
     */
    public String moveFollowGroup() {
        s = myFollowService.updateFollowGroup((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),fuidStr, fgid);
        return SUCCESS;
    }
    
    /**
     * 新增分组
     * @return
     */
    public String addFollowGroup() {
        s = myFollowService.insertFollowGroup((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), name);
        return SUCCESS;
    }
    
    /**
     * 求关注
     * @return
     */
    public String requestFollow() {
        s = myFollowService.insertRFLetter((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fuid,content);
        return SUCCESS;
    }
    
    /**
     * 获取关注分组的新鲜事
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFreshByFgid() {
        //searchFreshByFgid
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = myFollowService.searchFreshByFgid(page, page_size, 
                (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),fgid);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            freshList = (List<Fresh>) map.get(ICommonDAO.OBJ_LIST);
            
            if (null != freshList && freshList.size() > 0) {
                for (Fresh fresh : freshList) {
                    if (fresh.getType() != 0) {
                        fresh.setFresh(homeForumsService.searchFresh(fresh.getFid(),1));
                    }
                }
            }
                
        }
        uid = (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取关注的动态信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFollowDo() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = myFollowService.searchFollowDo(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),fgid);
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            followList = (List<Follow>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 判断是否已关注
     */
    public String searchFollowByFuid() {
        s = myFollowService.searchFollowByUid((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), in_uid);
        return SUCCESS;
    }
    
    /**
     * 获取所有的关注，@时用
     * @return
     */
    public String searchFollowForSpecial() {
        followList = myFollowService.searchFollowForSpecial((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        s = 1;
        return SUCCESS;
    }
    
    // 新版
    /**
     * 获取所有的好友(state=2)、关注(state=1)、粉丝(state=3)
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAllFriend() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        
        Map<String, Object> map = myFollowService.searchAllFriend(page, page_size, uid, state);
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer)map.get(ICommonDAO.COUNT);
            page = (Integer)map.get(ICommonDAO.PAGE);
            followList = (List<Follow>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 添加关注
     * @return
     */
    public String insertFollow() {
        int i= myFollowService.insertFollow(uid, fuid);
        if(i==1){
        	com.aier.user.entity.User userF=userService.getAppUid(fuid);
        	com.aier.user.entity.User userMy=userService.getAppUid(uid);
            List<BindInfo> bf=channelClient.QueryBindList(userF.getAppUid());
            channelClient.PushUnicastNotification("an",bf.get(0).getChannelId(),bf.get(0).getUserId(),"系统消息",userMy.getNickname()+"也关注了你,现在你们是好友了~",AndroidType.SYSTEMMESSAGE, uid);
        }
        s=1;
        return SUCCESS;
    }
    
    public void setPage(int page) {
        this.page = page;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getS() {
        return s;
    }

    public void setMyFollowService(IMyFollowService myFollowService) {
        this.myFollowService = myFollowService;
    }

    public List<FollowGroup> getFgList() {
        return fgList;
    }

    public int getFollowCount() {
        return followCount;
    }

    public List<Follow> getFollowList() {
        return followList;
    }

    public void setFuidStr(String fuidStr) {
        this.fuidStr = fuidStr;
    }

    public void setFuid(Long fuid) {
        this.fuid = fuid;
    }

    public void setFgid(Long fgid) {
        this.fgid = fgid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Fresh> getFreshList() {
        return freshList;
    }

    public void setHomeForumsService(IHomeForumsService homeForumsService) {
        this.homeForumsService = homeForumsService;
    }

    public Long getUid() {
        return uid;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public void setState(Short state) {
        this.state = state;
    }

    public Long getFgid() {
        return fgid;
    }

    public int getCount() {
        return count;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setIn_uid(Long in_uid) {
        this.in_uid = in_uid;
    }

    public int getPage() {
        return page;
    }

    public List<String> getNicknameList() {
        return nicknameList;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

	public Follow getFollow() {
		return follow;
	}
}